package actions;

import com.myapp.struts.Usuario;
import desarrollo.Form.form;
import com.myapp.struts.UsuarioHelper;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class action extends Action {

    @Override
    public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {

        form formulario = (form) form;
        String u = formulario.getUsername();
        String p = formulario.getPassword();
        
        UsuarioHelper usuario = new UsuarioHelper();
        List<UsuarioHelper> lista = usuario.getUsers();
        boolean exist = usuario.loginAccess(u, p, lista);
        if ((u.equals("") || u.equals(" ")) && p.equals("")) {
            return (mapping.findForward("cambio3"));
        }

        if ((u.equals("") || u.equals(" ")) || p.equals("")) {
            return (mapping.findForward("cambio"));
        }

        if (exist) {
            Usuario cliente = usuario.infoUser(u);
            if (cliente.getCategoria().equals("Administrador")){
                System.out.println("Usuario Logeado");
                return (mapping.findForward("cambio2"));}
            else{return (mapping.findForward("cambio4"));}
        } else {
            System.out.println("Error Loggin");
            return (mapping.findForward("cambio"));
        }
    }
}
